package com.example.SBootDemo.service.Imp.function;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.example.SBootDemo.Dao.Interface.moduleDao;
import com.example.SBootDemo.ExceptionHandel.exception.Myexception;
import com.example.SBootDemo.Util.TableTitles;
import com.example.SBootDemo.Util.funplace;
import com.example.SBootDemo.model.function.role;
import com.example.SBootDemo.model.function.topmodule;
import com.example.SBootDemo.model.function.user;
import com.example.SBootDemo.service.Interface.function.topmoduleinterface;
@Service
public class topmoduleimpl implements topmoduleinterface {

	@Autowired
	moduleDao modeled;
	//modellist
	@Override
	public int getModules(int pageinfo, HttpServletRequest request) throws Myexception {
		// TODO Auto-generated method stub
		int flag=0;
		
		Sort sort = new Sort(Direction.DESC, "id");
	    Pageable pageable = new PageRequest(pageinfo, TableTitles.getPagesize(), sort);
	    Page page = null;
		
			try {
				page = modeled.findAll(pageable);
				flag=1;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new Myexception("分页查询module方法出错");
			}
			 
		    //面包屑导航
	    funplace funplace = new funplace("系统管理","用户与权限","顶栏模块管理-顶栏模块列表");
	    List<String> titles = TableTitles.getModuleTitles();
	    //列表信息
		 request.setAttribute("List", page.getContent());
		//列表栏目信息
		 request.setAttribute("titles", titles);
		 //分页总页数
		 request.setAttribute("pages", page.getTotalPages());
		 //当前页，spring date jpa默认从0算起
		 request.setAttribute("number", page.getNumber());
	     request.setAttribute("funplace", funplace);
		return  flag;
	}
	
	
	
/***************************跳转update页面********************************************/
	@Override
	public void addpage(HttpServletRequest request) {
		// TODO Auto-generated method stub
		  funplace funplace = new funplace("系统管理","用户与权限","模块添加");
		  request.setAttribute("funplace", funplace);
		  //空user 前台模板取null有用
		  request.setAttribute("topmodule", new topmodule());
		  
	}


	/***************************模块删除**************************************/
	@Override
	@Transactional
	public void deleteFun(List<Integer> listid) throws Myexception {
		// TODO Auto-generated method stub
		try {
			for(int i=0;i<listid.size();i++){
				modeled.deleteById(listid.get(i));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Myexception("删除模块出错");
		}
	}


	/***************************模块修改页面**************************************/
	@Override
	public void updatepage(HttpServletRequest request, int id) {
		// TODO Auto-generated method stub
		topmodule topmodule = modeled.findById(id);
		 funplace funplace = new funplace("系统管理","用户与权限","模块修改");
		
		 request.setAttribute("topmodule", topmodule);
	     request.setAttribute("funplace", funplace);
		 
	}

	/***************************模块修改**************************************/

	@Override
	public void updateModule(topmodule module, HttpServletRequest request) throws Myexception {
		// TODO Auto-generated method stub
		
		
		try {
			modeled.save(module);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Myexception("更新模块信息方法出错");
		}
	}


	/***************************根据当前用户得到拥有模块(暂时全部查出)********************/
	@Override
	public List<topmodule> findByUser() {
		// TODO Auto-generated method stub
		return modeled.findAll();
	}
	
}
